package com.mhkj.base.itemInfo.phase.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.itemInfo.group.entity.Group;
import com.mhkj.base.itemInfo.group.service.GroupService;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.itemInfo.phase.entity.Phase;
import com.mhkj.base.itemInfo.phase.entity.TreePhaseVo;
import com.mhkj.base.itemInfo.phase.service.PhaseService;
import com.mhkj.base.region.entity.TSysNation;
import com.mhkj.base.region.service.NationService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 期区
 */

@Controller
@RequestMapping("/phase")
public class PhaseController extends BaseController<Phase> {
	@Autowired
	private PhaseService phaseService;
	@Autowired
	private NationService nationService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private GroupService groupService;
	
	/**
	 * 分页查询期区
	 * 
	 */
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<Phase> listByPage( Phase Phase, int pageIndex, int pageSize ) {
		Pagination<Phase> pagination = phaseService.listByPage(Phase,pageIndex, pageSize);
		return pagination;
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(Phase Phase) {
		
		Long cityId = Phase.getCityId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "城市为空!");
		}
		TSysNation nation = nationService.getById(cityId);
		Phase.settSysNation(nation);
		Long itemId = Phase.getItemId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "项目为空!");
		}
		Item item = itemService.getById(itemId);
		Phase.setItem(item);
		
		 Long groupId = Phase.getGroupId();
		if(!StringUtils.isEmpty(groupId)) {
			Group group = groupService.getById(groupId);
			Phase.setGroup(group);
		}
		
		String message = phaseService.save(Phase);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(Phase Phase) {
		Long cityId = Phase.getCityId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "城市为空!");
		}
		TSysNation nation = nationService.getById(cityId);
		Phase.settSysNation(nation);
		Long itemId = Phase.getItemId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "项目为空!");
		}
		Item item = itemService.getById(itemId);
		Phase.setItem(item);
		String message = phaseService.edit(Phase);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del",method={RequestMethod.POST})
	@ResponseBody
	public ResponseMessage del(Long id) {
		if(StringUtils.isEmpty(id)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "id为空!");
		}
		Phase Phase = new Phase();
		Phase.setId(id);
		String message = phaseService.del(Phase);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 查询所有项目期区树
	 * @throws Exception 
	 * @throws IllegalAccessException 
	 * 
	 */
	@ResponseBody
	public List<TreePhaseVo> listAll(Long cityId,String itemName,Long provinceId) throws Exception {
		
		List<Phase> list = phaseService.findByCidOrItemName(cityId,itemName,provinceId);
		
		List<TreePhaseVo> listVo = new ArrayList<TreePhaseVo>();
		HashSet<Item> set = new HashSet<Item>();
		
		for (Phase phase : list) {
			
			Employee employeeInfo = orgService.employeeInfo(phase.getCreateBy());
			TreePhaseVo vo = new TreePhaseVo();
			vo.setCreateName(employeeInfo.getName());
			Item item = phase.getItem();
			set.add(item);
			if(!StringUtils.isEmpty(item)) {
				vo.setPid(item.getId());
			}
			BeanUtils.copyProperties(vo,phase);
			listVo.add(vo);
		}
		
		for (Item item : set) {
			TreePhaseVo vo = new TreePhaseVo();
			vo.setId(item.getId());
			vo.setPhaseName(item.getName());
			Employee employeeInfo = orgService.employeeInfo(item.getCreateBy());
			vo.setCreateName(employeeInfo.getName());
			vo.setCreateDate(item.getCreateDate());
			listVo.add(vo);
		}
		
		
		
		
		return listVo;
	}
	
	/**
	 * 查询期区信息
	 */
	@RequestMapping(value="/info", method=RequestMethod.POST)
	@ResponseBody
	public Phase info(@RequestParam(value = "id", required = true) long id) {
		Phase Phase = phaseService.getById(id);
		return Phase;
	}


	/**
	 * 批量删除期区
	 */
	@RequestMapping(value="/delAll", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delAll(@RequestParam(value="id",required=true) String id) {
		String[] split = id.split(",");
		for (String str : split) {
			long parseLong = Long.parseLong(str);
			Phase Phase = phaseService.getById(parseLong);
			phaseService.del(Phase);
		}
		
		return ResponseMessage.createMessage(true, "ok");
	}
	/**
	 * 通过项目id查询非组团期区
	 */
	@RequestMapping(value="/findPhaseByItemId", method=RequestMethod.POST)
	@ResponseBody
	public List<Phase> findPhaseByItemId(Long itemId) {
		 List<Phase> listPhase = phaseService.findPhaseByItemId(itemId);
		 
		return listPhase;
	}
 
	/**
	 * 通过组团id查询该组团下的期区期区
	 */
	@RequestMapping(value="/findPhaseByGroupId", method=RequestMethod.POST)
	@ResponseBody
	public List<Phase> findPhaseByGroupId(Long groupId) {
		List<Phase> listPhase = phaseService.findPhaseByGroupId(groupId);
		return listPhase;
	}
	
	
	

 
	
}
